<?php
error_reporting(0);
require_once 'function.php';
$hd  = isset($_GET['hd']) ? addslashes($_GET['hd']) : 2;
$vid = isset($_GET['vid']) ? addslashes($_GET['vid']) : "";

$data['code'] = 200;
$data['source'] = '1haza';
$data['request_vid'] = $vid;

if(!strstr($vid, ':')) {
	$data['code'] = 404;
	$data['message'] = '解析失败或资源部存在';
	echo json_encode_jsonify($data); die;
}

// 缓存默认为1小时
$cached = new Cached();
$play_url  = $cached->get($vid);
if(!$play_url) {
	// 获取缓存的真实优酷开放云点播VID
	$yvid = fast_cache($vid);
	if(!$yvid) {
		// 没有就获取
		$cookie = '6133DE07EC77D4F21775557CE1D983FD3E381A6F9CC60E297D7E7CF0CF7F5AD7BA15240F818207837387EE622B81FE653D6935D870265941811667EC8BF822423E67F3EDBF4F73BEA6EC92D472952D3689A52025CF061937B3C3DFCB86285F4C';
		$urls = post('http://v.82ucc.com/buy', 'film='.strzuo($vid, ':').'&chapter='.stryou($vid, ':').'&buy=true', 'my_login_cookie='.$cookie)->body;
		if(!strstr($urls, 'vod.open.youku.com')) {
			$data['code'] = 404;
			$data['message'] = '解析失败!';
			echo json_encode_jsonify($data); die;
		}
		$yvid = strzuo(stryou($urls, 'vid='), '&');
		// 永久存储真实VID
		fast_cache($vid, $yvid);
	}
	$info = post('http://vod.open.youku.com/videoinfo/public_video_stream_v2?vid='.$yvid.'&cid=57e0843ae4b0eaf5dd323234', null, '__ysuid=1476924524506Y5P; __vtft=1476924524620')->body;
	$info = substr(substr($info,0,-1),1);
	$info = json_decode($info, true);
	$play_url = $info['data']['streams'][0]['muxers'][0]['slices'][0]['play_url'];
	$cached->set($vid, $play_url);
}

if(empty($play_url)) {
	$data['code'] = 404;
	$data['message'] = '解析失败!';
	echo json_encode_jsonify($data); die;
}

$data['result']['files'] = $play_url;
$data['result']['play_type'] = 'mp4';
$data['result']['definition'] = $hd;
$data['result']['definitionList'] = '1标清|2高清|3超清';
$data['message'] = '操作正常';
echo json_encode_jsonify($data);